package cn.lutu.lutu.controller;


import cn.lutu.lutu.response.ResultVO;
import cn.lutu.lutu.security.CustomUserDetails;
import cn.lutu.lutu.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@Api(tags = "上传文件模块")
@RequestMapping("/v1/uploads/")
public class UploadController {

    @Value("${filePath}")
    private String filePath;
    @Autowired
    UploadService uploadService;
    @ApiOperation("上传文件")
    @PostMapping("")
    public ResultVO upload(MultipartFile file, @AuthenticationPrincipal CustomUserDetails userDetails) throws IOException {
       log.info("开始处理上传文件业务");
        String datePath = uploadService.getDatePath();
       String fileName = uploadService.getFileName(file);
       File dirFile=new File(filePath+datePath);
       log.info("即将准备进入创建文件");
        if(!dirFile.exists()){
            log.info("创建文件");
            dirFile.mkdirs();
        }
        file.transferTo(new File(filePath+datePath+fileName));
        return ResultVO.ok(datePath+fileName);
    }

    @ApiOperation("删除文件")
    @GetMapping("remove")
    public ResultVO remove(String url){
        log.info(("开始删除文件"));
        new File(filePath+url).delete();
        return ResultVO.ok();
    }

}
